/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.border.Border;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class ToolBarButton extends JButton {

	public static final int defaultWidth = 25;
	public static final int defaultHeight = 24;
	private Border normalBorder;
	private Border rolloverBorder;

	public ToolBarButton(Icon icon) {
		this(icon, 25, 24);
	}

	public ToolBarButton(Action action) {
		super(action);
		this.normalBorder = BorderFactory.createEmptyBorder();
		this.rolloverBorder = BorderFactory.createEtchedBorder();
		Dimension dimension = new Dimension(25, 24);
		setMaximumSize(dimension);
		setMinimumSize(dimension);
		setPreferredSize(dimension);
		setSize(dimension);
		setText("");
	}

	public ToolBarButton(Icon icon, int width, int height) {
		this.normalBorder = BorderFactory.createEmptyBorder();
		this.rolloverBorder = BorderFactory.createEtchedBorder();
		if (icon != null) {
			setIcon(icon);
		}
		setOpaque(false);
		setBorder(null);
		Dimension dimension = new Dimension(width, height);
		setMaximumSize(dimension);
		setMinimumSize(dimension);
		setPreferredSize(dimension);
		setSize(dimension);
	}

	@Override
	protected void paintBorder(Graphics g) {
		if (getBorder() != null) {
			getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
		} else if (getModel().isRollover()) {
			this.rolloverBorder.paintBorder(this, g, 0, 0, getWidth(), getHeight());
		} else {
			this.normalBorder.paintBorder(this, g, 0, 0, getWidth(), getHeight());
		}
	}
}